package io.sage.modules.query.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.sage.modules.query.dao.BAreaConfigMapper;
import io.sage.modules.query.dao.BWeatherMapper;
import io.sage.modules.query.entity.BAreaConfig;
import io.sage.modules.query.entity.BWeather;
import io.sage.modules.query.service.WeatherService;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.net.www.http.HttpClient;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zxh
 * @Title: WeatherServiceImpl
 * @ProjectName sage_wms
 * @Description: TODO
 * @date 2019/8/1416:13
 */
@Service
public class WeatherServiceImpl implements WeatherService {
    @Value("${weather.url}")
    private String url;

    @Value("${weather.uri}")
    private String uri;
    @Autowired
    private BAreaConfigMapper bAreaConfigMapper;
    @Autowired
    private BWeatherMapper bWeatherMapper;
    @Override
    public void readWeather() {
        BAreaConfig bAreaConfig=new BAreaConfig();
        bAreaConfig.setIsDel(0);
        List<BAreaConfig> list=bAreaConfigMapper.queryAll(bAreaConfig);

        //创建并使用HTTPget请求方式
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse execute = null;
        try {
        for (int i = 0; i < list.size(); i++) {
            HttpGet httpGet = new HttpGet(url +list.get(i).getLatitude()+","+list.get(i).getLongitude()+ uri);
            //获取内容并封装到HTTPentity
            execute = httpClient.execute(httpGet);
            HttpEntity entity = execute.getEntity();
            String json=EntityUtils.toString(entity);
            json=json.substring(11, json.length()-1);
            JSONObject jsonObject=JSONObject.parseObject(json);
            JSONObject result=jsonObject.getJSONObject("result");
            JSONObject daily=result.getJSONObject("daily");
            String  skycon =daily.getJSONArray("skycon").getJSONObject(0).getString("value");
            String  date =daily.getJSONArray("skycon").getJSONObject(0).getString("date");
            Double  avg =daily.getJSONArray("temperature").getJSONObject(0).getDouble("avg");
            Double  min =daily.getJSONArray("temperature").getJSONObject(0).getDouble("min");
            Double  max =daily.getJSONArray("temperature").getJSONObject(0).getDouble("max");
            BWeather weather=new BWeather();
            weather.setWeatherDate(new Date());
            weather.setTemperatureAvg(avg);
            weather.setTemperatureMax(max);
            weather.setTemperatureMin(min);
            weather.setSkycons(skycon);
            weather.setRemark(json);
            bWeatherMapper.insert(weather);
        }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (execute != null) {
                    execute.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

    @Override
    public List selectByBWeather(BWeather bWeather) {
        return this.bWeatherMapper.queryAll(bWeather);
    }
}
